package cn.relx.basic.service.impl;

import cn.relx.basic.mapper.BaseMapper;
import cn.relx.basic.query.BaseQuery;
import cn.relx.basic.service.BaseService;
import cn.relx.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements BaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;
    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public PageList<T> queryByPage(BaseQuery query) {
        // 查询条数
        Integer totals = mapper.queryByCount(query);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        // 查询数据
        List<T> ts = mapper.queryByPage(query);

//        result.setMsg("true");
//        result.setSuccess(true);
//        return result
        //方到返回里面
        return new PageList<>(totals,ts);
    }

    @Override
    @Transactional
    public void batchDel(List<Long> ids) {
        mapper.batchDel(ids);
    }

}
